﻿using System;
using System.Collections.Generic;
using System.Text;

namespace StandXunYouWeb.Utility
{
    public class CommonHelpers
    {
        #region 产生10位随机码&产生6位数字随机数

        //英文随机数
        private static char[] constant =
                {
                '0','1','2','3','4','5','6','7','8','9',
                'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
                'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
                };
        /// <summary>
        /// 随机生成英文
        /// </summary>
        /// <param name="Length">长度</param>
        /// <returns></returns>
        public static string GenerateRandom(int Length)
        {
            StringBuilder newRandom = new StringBuilder(62);
            Random rd = new Random();
            for (int i = 0; i < Length; i++)
            {
                newRandom.Append(constant[rd.Next(62)]);
            }
            return newRandom.ToString();
        }

        //生成随机数
        public static String getCard()
        {
            Random rand = new Random();//生成随机数
            String cardNnumer = "";
            for (int a = 0; a < 6; a++)
            {
                cardNnumer += rand.Next(10);//生成6位数字
            }
            return cardNnumer;
        }

        public static string GenerateNonceStr()
        {
            int ran = 0;
            byte[] r = new byte[32];
            Random rand = new Random((int)(DateTime.Now.Ticks % 1000000));
            //生成8字节原始数据
            for (int i = 0; i < 32; i++)
                //while循环剔除非字母和数字的随机数
                do
                {
                    //数字范围是ASCII码中字母数字和一些符号
                    ran = rand.Next(48, 122);
                    r[i] = Convert.ToByte(ran);
                } while ((ran >= 58 && ran <= 64) || (ran >= 91 && ran <= 96));
            //转换成8位String类型               
            string nonceStr = Encoding.ASCII.GetString(r);
            nonceStr = nonceStr.ToUpper();

            //RandomGenerator randomGenerator = new RandomGenerator();
            //randomGenerator.GetRandomUInt().ToString();
            return nonceStr;
        }
        public static string GenerateOutTradeNo()
        {
            var ran = new Random();
            return string.Format("{0}{1}",  DateTime.Now.ToString("yyyyMMddHHmmss"), ran.Next(999));
        }
        #endregion
    }
}
